<?php
namespace App\Http\Controllers;


use App\Exceptions\ApiException;
use App\Http\Requests\MsgPushRequest;
use App\Http\Requests\MsgSendRequest;
use App\Libraries\msg\Msg;
use App\Libraries\msg\Push;

class MsgController extends CommonController {
	
	protected $template = [
		'验证码'			=> '145450',
		'费用催缴'		=> '182478',
		'停水通知'		=> '182598',
		'停电通知'		=> '182599',
		'放假通知'		=> '182600',
        '派单通知'    => '236228',
        '新派单通知'    => '230141',
        '付款通知'      =>  '230146',
        '审核通知'      =>  '232484',
        '审核失败通知'  =>  '232482',
	];
	
	// 队列发送接口
	public function message(MsgSendRequest $request){
		$msg = new Msg();
		$mobile = $request->input('mobile');
		$template = $request->input('template');
		$param = $request->input('param');
		$template_code = $this->template[$template];
		if(empty($template_code)){
			throw new ApiException('手机短信模板编号未找到');
		}
		$return = $msg->queue($mobile,$template_code,$param);
		return $return ? 'success' : 'fail';
	}
	
	// 实时发送接口，用于接口调试用
	public function message_sync(MsgSendRequest $request){
		$msg = new Msg();
		$mobile = $request->input('mobile');
		$template = $request->input('template');
		$param = $request->input('param');
		$template_code = $this->template[$template];
		if(empty($template_code)){
			throw new ApiException('手机短信模板编号未找到');
		}
		$return = $msg->send($mobile,$template_code,$param);
		return $return ? 'success' : 'fail';
	}
	
	// 队列发送APP推送
	public function push(MsgPushRequest $request){
		$push = new Push();
		$uid = $request->input('uid');
		$title = $request->input('title');
		$content = $request->input('content');
		$return = $push->queue($uid,$title,$content);
		return $return ? 'success' : 'fail';
	}
	
	// 实时同步发送APP推送
	public function push_sync(MsgPushRequest $request){
		$push = new Push();
		$uid = $request->input('uid');
		$title = $request->input('title');
		$content = $request->input('content');
		$return = $push->send($uid,$title,$content);
		return $return ? 'success' : 'fail';
	}
}